<?php 
require_once "class.db.php";
class trangchu extends db{	
	function TheLoai_List($limit=-1,$offset=-1){
		$sql = "SELECT * FROM theloai ORDER BY idTL ";
		if($limit >0 && $offset >=0) $sql.= " LIMIT $offset,$limit";
		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;
	}
	
	function LoaiTin_List($idTL=-1,$limit=-1,$offset=-1){
		$sql = "SELECT loaitin.* FROM loaitin 
				WHERE (loaitin.idTL = $idTL OR $idTL = -1 )
				ORDER BY idLT ASC ";
		if($limit >0 && $offset >=0) $sql.= " LIMIT $offset,$limit";		
		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;
	}  
	function getDetailSP($sp_id){
		$sql= "SELECT sanpham.*,TenLT 
			FROM sanpham,loaitin
			WHERE idSP = $sp_id AND sanpham.idLT = loaitin.idLT";
		$rs= mysql_query($sql) or die(mysql_error());
		return $rs;
	}
   	function SanPham_List($loai_id =-1,$tukhoa='',$limit=-1,$offset=-1){
		$sql = "SELECT * FROM sanpham 
				WHERE (idLT = $loai_id OR $loai_id = -1 )";
		if($tukhoa!="") $sql.=" AND MaSP LIKE '%$tukhoa%' ";															
			$sql.="	ORDER BY idSP DESC ";
		if($limit >0 && $offset >=0) $sql.= " LIMIT $offset,$limit";

		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;					
	}
	function SanPham_BanChay($limit=-1,$offset=-1){
		$sql = "SELECT * FROM sanpham 
				WHERE BanChay=1 ";														
			$sql.="	ORDER BY idSP DESC ";
		if($limit >0 && $offset >=0) $sql.= " LIMIT $offset,$limit";

		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;					
	}	
	function SanPham_Moi($limit=-1,$offset=-1){
		$sql = "SELECT * FROM sanpham 
				WHERE AnHien=1 ORDER BY idSP DESC ";
		if($limit >0 && $offset >=0) $sql.= " LIMIT $offset,$limit";

		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;					
	}
	function SanPham_CungLoai($idTL,$idSP,$limit=-1,$offset=-1){
		$sql = "SELECT * FROM sanpham 
				WHERE AnHien=1 AND idTL = $idTL AND idSP <> $idSP ORDER BY idSP DESC ";
		if($limit >0 && $offset >=0) $sql.= " LIMIT $offset,$limit";

		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;					
	}		
	function chitiet_loaitin($idLT){
		$sql = "SELECT loaitin.*,TenTL FROM loaitin,theloai WHERE idLT  = $idLT AND loaitin.idTL = theloai.idTL";
		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;
	}
        function chitiet_sanpham($idSP){
		$sql = "SELECT * FROM sanpham WHERE idSP  = $idSP";
		$rs = mysql_query($sql) or die(mysql_error());
		return $rs;
	}
	function GetNoiDung($id){
		$sql = "SELECT NoiDung FROM noidung WHERE idND = $id";
		$rs = mysql_query($sql);
		$row = mysql_fetch_assoc($rs);
		$noidung = $row['NoiDung'];
		return $noidung;
	}
        function Tin_List($limit = -1, $offset = -1) {
            $sql = "SELECT idTin,TieuDe,UrlHinh,TomTat,TieuDe_KD FROM tin 
                                    WHERE 0 = 0 ";           
            $sql.="	ORDER BY idTin DESC ";
            if ($limit > 0 && $offset >= 0)
                $sql.= " LIMIT $offset,$limit";
            $rs = mysql_query($sql) or die(mysql_error());
            return $rs;
        }
        function loai_theotheloai($idTL){
            $sql = "SELECT * FROM loaitin 
                                    WHERE idTL = $idTL ORDER BY idLT ASC";           
            $rs = mysql_query($sql) or die(mysql_error());
            return $rs;
        }
        function sanpham_theoloai($idLT,$offset,$limit){
            $sql = "SELECT * FROM sanpham 
                                    WHERE idLT = $idLT ";           
            $sql.="	ORDER BY idSP DESC ";
            if ($limit > 0 && $offset >= 0)
                $sql.= " LIMIT $offset,$limit";
            $rs = mysql_query($sql) or die(mysql_error());
            return $rs;
        }
        function sanpham_theochungloai($idTL,$offset,$limit){
            $sql = "SELECT * FROM sanpham 
                                    WHERE idTL = $idTL ";           
            $sql.="	ORDER BY idSP DESC ";
            if ($limit > 0 && $offset >= 0)
                $sql.= " LIMIT $offset,$limit";
            $rs = mysql_query($sql) or die(mysql_error());
            return $rs;
        }
        function sanpham_noibat(){
            $sql = "SELECT * FROM sanpham WHERE noibat = 1 ORDER BY idSP DESC LIMIT 0,4";
            $rs = mysql_query($sql);
            return $rs;
        }
		
}

?>